package com.elhakimz.matra.actor;

import com.elhakimz.matra.geo.GeoShape;
import com.elhakimz.matra.util.GeoUtil;
import com.elhakimz.matra.util.GlobalVars;
import com.elhakimz.matra.util.ModelUtil;
import com.elhakimz.matra.MatraSystem;
import com.jme.bounding.BoundingBox;
import com.jme.curve.BezierCurve;
import com.jme.curve.CurveController;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.BillboardNode;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;

/**
 * Simulation Actor data GL drawing
 */
public class ActorGL {

    private Node platform;
    private Node tact;
    private Node radarArc;
    private Node stat;
    private BillboardNode billboard;

    private CurveController actorController;
    private CurveController bbController;
    private CurveController arcController;


    Vector3f up = new Vector3f(0, 1, 0);
    private float speed = 0.1f;


    private String name;

    public ActorGL(String name, String model) {
        this.name = name;
        platform = ModelUtil.loadModel(model);
        platform.setName(name);
        billboard = new BillboardNode(name);
        System.out.println(platform);
    }

    public Node getPlatform() {
        return platform;
    }

    public Node getTact() {
        return tact;
    }

    public Node getStat() {
        return stat;
    }

    public Node getBillboard() {
        return billboard;
    }

    public Controller getActorController() {
        return actorController;
    }

    public String getName() {
        return name;
    }

    public float getSpeed() {
        return speed;
    }

    public void setSpeed(float speed) {
        this.speed = speed;
    }

    public void attach() {
        ModelUtil.scaleModel(platform, 0.01f);
        platform.setModelBound(new BoundingBox());
        platform.updateModelBound();
        platform.setLightCombineMode(Spatial.LightCombineMode.Inherit);
        platform.setRenderQueueMode(Renderer.QUEUE_OPAQUE);

        TextureState ts = MatraSystem.disp.getRenderer().createTextureState();
        ts.setEnabled(true);
        ts.setTexture(
                TextureManager.loadTexture(
                        ActorGL.class.getClassLoader().getResource(
                                "jmetest/data/texture/Detail.jpg"),
                        Texture.MinificationFilter.BilinearNearestMipMap,
                        Texture.MagnificationFilter.Bilinear));
        platform.setRenderState(ts);

        setBB();
        GlobalVars.tracksNode.attachChild(platform);
        GlobalVars.pickNode.attachChild(platform);
    }

    public void setPos(float lat, float lon, float alt) {
        platform.setModelBound(new BoundingBox());
        platform.updateModelBound();
        platform.setLocalTranslation(GeoUtil.GV3f(lat, lon, alt));
        billboard.setLocalTranslation(GeoUtil.GV3f(lat, lon, alt + 5));
    }

    public void setBB() {

        setRadar();
        Quad q = new Quad("Quad");

        q.updateGeometry(2.5f, 2.5f);
        q.setLocalTranslation(0, 5, 0);
        TextureState ts = MatraSystem.disp.getRenderer().createTextureState();
        ts.setEnabled(true);
        Texture t1 = TextureManager.loadTexture(
                ActorGL.class.getClassLoader().getResource(
                        "resources/matra/images/air_fw_friendly.png"),
                Texture.MinificationFilter.Trilinear,
                Texture.MagnificationFilter.Bilinear);
        ts.setTexture(t1);
        q.setRenderState(ts);
        q.setDefaultColor(ColorRGBA.blue);

        billboard.setAlignment(BillboardNode.CAMERA_ALIGNED);
        billboard.attachChild(q);

        GlobalVars.sceneNode.attachChild(billboard);
        GlobalVars.pickNode.attachChild(billboard);
    }

    public void setRadar() {
        Vector3f v = platform.getLocalTranslation();
        radarArc = GeoShape.Sector2D(v, 180, 100, 120, 0);
        radarArc.setName(this.name + "-radar");
        platform.attachChild(radarArc);
        GlobalVars.sceneNode.attachChild(radarArc);

    }

    //initialize, load model
    public void followWaypoints(Vector3f[] points) {
        BezierCurve curve = new BezierCurve("Curve", points);
        curve.setSolidColor(ColorRGBA.gray);
        //todo set the heading of platform to first vector.

        actorController = new CurveController(curve, platform);
        bbController = new CurveController(curve, billboard);
        arcController = new CurveController(curve, radarArc);

        platform.addController(actorController);
        billboard.addController(bbController);
        radarArc.addController(arcController);

        actorController.setRepeatType(Controller.RT_WRAP);
        actorController.setSpeed(speed);
        actorController.setAutoRotation(true);


        bbController.setRepeatType(Controller.RT_WRAP);
        bbController.setSpeed(speed);

        arcController.setRepeatType(Controller.RT_WRAP);
        arcController.setSpeed(speed);

        //GlobalVars.sceneNode.attachChild(curve);

    }

    //move along a line controller to v3f in speed
    //move along a curve controller to v3f in speed
    //rotate along object x-axis

    private void roll(float angle) {

    }

    //yaw the platform
    //rotate along object y-axis
    private void yaw(float angle) {

    }

    //picth the platform
    //rotate along object z-axis

    private void pitch(float angle) {

    }

    //head to a vector
    private void headTo(Vector3f vector) {

    }


}
